package com.anpy.bill.controller;

import com.anpy.bill.entity.*;
import com.anpy.bill.model.param.BillListParams;
import com.anpy.bill.model.result.BillStatisticsResult;
import com.anpy.bill.model.result.MonthMoneyResult;
import com.anpy.bill.service.BillClassifyService;
import com.anpy.bill.service.BillSecondClassifyService;
import com.anpy.bill.service.BillService;
import com.anpy.common.service.model.result.ApiResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author : anpy
 * @create 2023/10/26 15:29
 */
@RestController
@RequestMapping("/bill")
@ResponseBody
public class BillController {

    @Resource
    private BillClassifyService billClassifyService;
    @Resource
    private BillSecondClassifyService billSecondClassifyService;
    @Resource
    private BillService billService;

    @PostMapping("/classify/save")
    public ApiResult<String> saveClassify(String classifyName) {
        boolean saveResponse = billClassifyService.saveClassify(classifyName);
        if (!saveResponse) {
            return ApiResult.failed("分类已存在");
        }
        return ApiResult.success();
    }

    @PostMapping("/secondClassify/save")
    public ApiResult<String> saveSecondClassify(String classifyName, Integer classifyId) {
        boolean saveResponse = billSecondClassifyService.saveSecondClassify(classifyName, classifyId);
        if (!saveResponse) {
            return ApiResult.failed("分类已存在");
        }
        return ApiResult.success();
    }

    @GetMapping("/classify/list")
    public ApiResult<List<ClassifyEntity>> classifyList() {
        return ApiResult.success(billClassifyService.classifyList());
//        return ApiResult.success("list");
    }

    @GetMapping("/all-classify/list")
    public ApiResult<List<Map<String, Object>>> allClassifyList(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<Map<String, Object>> classifyResultEntities = billClassifyService.queryClassifyList(-1);
        return ApiResult.success(classifyResultEntities);
    }

    @GetMapping("/secondClassify/list")
    public ApiResult<List<SecondClassifyEntity>> secondClassifyList(Integer classifyId) {
        return ApiResult.success(billSecondClassifyService.secondClassifyList(classifyId, 1));
    }

    @GetMapping("/month/list")
    public ApiResult<List<MonthMoneyResult>> billMonthList(Long userId, String year) {
        return ApiResult.success(billService.billMonthList(userId, year));
    }

    @GetMapping("/year/money")
    public ApiResult<MonthMoneyResult> billYearMoney(Long userId, String year) {
        return ApiResult.success(billService.billYearMoney(userId, year));
    }

    @GetMapping("/list")
    public ApiResult<BillListEntity> billList(BillListParams params) {
        return ApiResult.success(billService.billList(params.getUserId(), params));
    }

    @PostMapping("/save")
    public ApiResult<String> saveBill(@Validated @RequestBody BillEntity entity) {
        Boolean saveResponse = billService.saveBill(entity);
        if (!saveResponse) {
            return ApiResult.failed("保存失败");
        }
        return ApiResult.success("保存成功");
    }

    @PostMapping("/update")
    public ApiResult<String> updateBill(@Validated @RequestBody BillEntity entity) {
        Boolean saveResponse = billService.updateBill(entity);
        if (!saveResponse) {
            return ApiResult.failed("更新失败");
        }
        return ApiResult.success("更新成功");
    }

    @PostMapping("/delete")
    public ApiResult<String> deleteBill(@RequestBody BillDeleteParam billDeleteParam) {
        Boolean deleteResponse = billService.deleteBill(billDeleteParam);
        if (!deleteResponse) {
            return ApiResult.failed("删除失败");
        }
        return ApiResult.success("删除成功");
    }

    @GetMapping("/report")
    public ApiResult<List<BillReportResult>> report(@RequestBody BillReportParam billReportParam) {
        List<BillReportResult> deleteResponse = billService.report(billReportParam);
        return ApiResult.success(deleteResponse);
    }

    @GetMapping("/statistics")
    public ApiResult<BillStatisticsResult> statistics(Long userId) {
        BillStatisticsResult deleteResponse = billService.statistics(userId);
        return ApiResult.success(deleteResponse);
    }

    @GetMapping("/merchant/list")
    public ApiResult<List<MerchantEntity>> merchantList(Long userId) {
        List<MerchantEntity> merchantList = billService.merchantList(userId);
        return ApiResult.success(merchantList);
    }
}
